package com.estudos.init;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import exception.ConexaoException;

@Configuration
@ComponentScan("com.estudos")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
	@Bean
	public UrlBasedViewResolver setupViewResolver(){
		UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
        resolver.setPrefix("/WEB-INF/views/");  
        resolver.setSuffix(".jsp");  
        resolver.setViewClass(JstlView.class);  
        System.out.println("aaaaaaa views configuradas!");
        return resolver;
	}
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/root/**").addResourceLocations("//");
    }
	@Bean
	public EntityManager getEntityManager() throws ConexaoException{
		EntityManagerFactory factory;
		EntityManager manager = null;
		try {
			factory = Persistence.createEntityManagerFactory("estudos");
			manager = factory.createEntityManager();
			System.out.println("aaaaaaaaaaaaaa Criou o Manager Entity");
		} catch (Exception e) {
			e.printStackTrace();
			throw  new ConexaoException("Erro ao tentar criar Entity Manager!");
		}
		return manager;
	}
	
	
	
}
